﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Runda.EShop.Bll;
using System.Text;
using Runda.EShop.Common.Entity;
using Runda.EShop.Common.Utility;

namespace Runda.EShop.Web.Order
{
    public partial class OrderView : BasePage
    {
        public Guid OrderID
        {
            get
            {
                if (Request.QueryString["OrderID"] != null)
                {
                    return new Guid(Request.QueryString["OrderID"]);
                }
                return Guid.Empty;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            lnkbtnOrginalItem.Attributes.Add("onclick", "return OnOpenWinCustom('OrderDetailOrginal.aspx?OrderID=" + OrderID + "','原始订单查看',800,600);");
            if (!IsPostBack)
            {
                Initialize();
            }
        }

        private void Initialize()
        {
            BizOrderEntity entity = new OrderBll().Get<BizOrderEntity>(OrderID);
            if (entity.OrderStatus == (int)EnumHelper.OrderStatus.Cancel)
            {
                ltlProcess.Text = "<span style='font-size:15px;'>当前订单状态：" + EnumDescAttribute.GetFieldText(EnumHelper.OrderStatus.Cancel) + "</span>";
            }
            else
            {
                InitProcess();
            }

            GetOrderPatch();
            GetOrderProcess();
        }

        private void InitProcess()
        {
            string strFormat1 = "<div class=\"{0}\"><ul><li class=\"tx1\">&nbsp;</li><li class=\"tx2\">{1}</li><li class=\"tx3\">{2}</li></ul></div>";
            string strFormat2 = "<div class=\"{0}\"><ul><li class=\"tx1\">&nbsp;</li></ul></div>";

            BizOrderStatusBll bll = new BizOrderStatusBll();
            DataTable dt = bll.GetAllOrderStatus(OrderID);
            StringBuilder sb = new StringBuilder();
            int CurrentProcessStatus = 0, ProviouseProcessStatus = 0, NextProcessStatus = 0;
            int rowsCount = dt.Rows.Count;
            for (int i = 0; i < rowsCount; i++)
            {
                CurrentProcessStatus = Convert.ToInt32(dt.Rows[i]["ProcessStatus"].ToString());
                if (i == 0)
                {
                    ProviouseProcessStatus = 0;
                    if (rowsCount > 1)
                    {
                        NextProcessStatus = Convert.ToInt32(dt.Rows[i+1]["ProcessStatus"].ToString());
                    }
                }
                else
                {
                    if (i == (rowsCount - 1))
                    {
                        NextProcessStatus = 0;
                        if (rowsCount > 1)
                        {
                            ProviouseProcessStatus = Convert.ToInt32(dt.Rows[i - 1]["ProcessStatus"].ToString());
                        }
                    }
                    else
                    {
                        ProviouseProcessStatus = Convert.ToInt32(dt.Rows[i - 1]["ProcessStatus"].ToString());
                        NextProcessStatus = Convert.ToInt32(dt.Rows[i + 1]["ProcessStatus"].ToString());
                    }
                }

                if (CurrentProcessStatus == 1)
                {
                    sb.AppendLine(string.Format(strFormat1, "node ready", dt.Rows[i]["StatusName"].ToString(), dt.Rows[i]["CreatedAt"].ToString()));
                }
                else
                {
                    if (ProviouseProcessStatus == 1)
                    {
                        sb.AppendLine(string.Format(strFormat1, "node singular", dt.Rows[i]["StatusName"].ToString(), dt.Rows[i]["CreatedAt"].ToString()));
                    }
                    else
                    {
                        sb.AppendLine(string.Format(strFormat1, "node wait", dt.Rows[i]["StatusName"].ToString(), dt.Rows[i]["CreatedAt"].ToString()));
                    }
                }

                if (i < rowsCount - 1)
                {
                    if (CurrentProcessStatus == 1)
                    {
                        if(NextProcessStatus == 1)
                            sb.AppendLine(string.Format(strFormat2, "proce ready"));
                        else
                        sb.AppendLine(string.Format(strFormat2, "proce doing"));
                    }
                    else
                    {
                        sb.AppendLine(string.Format(strFormat2, "proce wait"));
                    }
                } 

            }

            ltlProcess.Text = sb.ToString();
        }

        private void GetOrderPatch()
        {
            if (OrderID == Guid.Empty) return;
            BizOrderDetailPatchBll bll = new BizOrderDetailPatchBll();
            DataTable dt = bll.GetOrderDetailPatch(OrderID);
            gvOrderPatch.DataSource = dt;
            gvOrderPatch.DataBind();
        }

        private void GetOrderProcess()
        {
            if (OrderID == Guid.Empty) return;
            List<BizOrderProcessEntity> list = new BizOrderProcessBll().GetAll<BizOrderProcessEntity>(" and OrderID='" + OrderID + "'", " ORDER BY ProcessTime asc");

            gvOrderProcess.DataSource = list;
            gvOrderProcess.DataBind();
        }
    }
}